﻿package com.best.oasis.settlement.biz.facade.security
{
	import com.best.oasis.flexbase.util.BeanUtils;
	import com.best.oasis.settlement.biz.facade.base.OldBaseFacade;
	import com.best.oasis.settlement.biz.facade.base.SuperResponder;
	import com.best.oasis.settlement.common.Constants;
	import com.best.oasis.settlement.common.so.security.UserSO;
	import com.best.oasis.settlement.common.vo.security.UserInfoFromLdapServerVO;
	import com.best.oasis.settlement.common.vo.security.UserVO;
	import com.best.oasis.settlement.util.page.BasePageSO;
	import com.best.oasis.settlement.util.page.PageList;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	/**
	 * 用户登录、注销等操作 
	 * @author bl00064
	 * 
	 */	
	public class UserFacade extends OldBaseFacade
	{
	

		/**
		 * 修改密码 
		 * @param oldPassword
		 * @param newPassword
		 * @param callback
		 * 
		 */
		public function updatePassword(oldPassword:String, newPassword:String, 
											 callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					var result:String = String(event.result);
					if(result==Constants.SUCCESS){
						Alert.show("密码修改成功！")
						if(callback != null){
							callback();
						}
					}else{
						Alert.show("密码修改失败!\n请检查原密码是否输入正确！")
					}
				}, 
				function(event:FaultEvent):void{
					Alert.show(event.fault.faultString);
				});
		
			delegate.updatePassword(oldPassword, newPassword).addResponder(responder);
		}
		/**
		 * 获取某个用户的角色列表
		 * @param userId
		 * @param result
		 * @param callback
		 * 
		 */
		public function getSelectedRoleGroup(userId:Number, result:ArrayCollection, 
											 callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					var tResult:ArrayCollection = ArrayCollection(event.result);
					if(event.result != null){
						BeanUtils.copyProperties(result, tResult);	
						if(callback!=null){
							callback(result); 					
						}
					}else{
						Alert.show("数据获取失败！");
						return;
					}
				}, 
				function(event:FaultEvent):void{
					Alert.show(event.fault.faultString);
				});
			
			delegate.getSelectedRoleGroup(userId).addResponder(responder);
		}
		/**
		 * 从LdapServer获得用户信息
		 * @param userName
		 * @param callback
		 * 
		 */
		public function getUserInfoFromLdapServer(userName:String, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(event.result != null){
						var result:UserInfoFromLdapServerVO = UserInfoFromLdapServerVO(event.result);
						if(callback!=null){
							callback(result); 
						}				
					}else{
						Alert.show("当前输入的用户不存在！");
						return;
					}
				}, 
				function(event:FaultEvent):void{
					Alert.show(event.fault.faultString);
				});
			
			delegate.getUserInfoFromLdapServer(userName).addResponder(responder);
		}
		
		public function create(vo:UserVO, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(event.result != null){
						Alert.show("保存成功！");
						if(callback != null){
							callback();
						}
					}
				}, 
				function(event:FaultEvent):void{
					trace(event.fault);
//					if(event.fault.rootCause['rootCause']['errorCode'] == 1){
//						Alert.show("添加用户失败！原因：用户已经存在.");
//					}else{
						Alert.show("保存失败！" + event.fault.faultString);
//					}
				});
			delegate.create(vo).addResponder(responder);
		}
		
		public function remove(ids:ArrayCollection, pageList:PageList, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(pageList != null){
						BeanUtils.removeByIds(pageList.list, ids);
						pageList.fullListSize -= ids.length;
					}
					if(callback != null){
						callback();
					}
				}, 
				function(event:FaultEvent):void{
					trace(event.fault);
					Alert.show("删除失败！");
				});
			
			delegate.removeUserprofile(ids).addResponder(responder);
		}
		public function update(vo:UserVO, pageList:PageList, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(pageList != null){
						var vo:UserVO = UserVO(event.result);
						BeanUtils.updateItem(pageList.list, vo);
					}
					if(callback != null){
						callback();
					}
				}, 
				function(event:FaultEvent):void{
					trace(event.fault);
					Alert.show("保存失败！" + event.fault.faultString);
				});
			
			delegate.update(vo).addResponder(responder);
		}
		/**
		 * 判断登录名是否存在
		 */
		public function userNameExist(userName:String, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(event.result !=null){
						if(callback != null){
							callback(event.result as Boolean);
						}  
					}
				},
				function(event:FaultEvent):void{
					trace(event.fault);
					Alert.show("获取数据失败！" + event.fault.faultString);
				});
			
			delegate.userNameExist(userName).addResponder(responder);
		}
		public function getPageList(so:UserSO, result:PageList, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					var tResult:PageList = PageList(event.result);
					BeanUtils.copyProperties(result, tResult);
				}, 
				function(event:FaultEvent):void{
					trace(event.fault);
					Alert.show("获取列表失败！" + event.fault.faultString);
				});
			
			delegate.getUserPageList(so).addResponder(responder);
		}
		/**
		 * 密码重置
		 */
		public function resetPassword(vo:UserVO, pageList:PageList, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(event.result !=null){
						if(pageList != null){
							var vo:UserVO = UserVO(event.result);
							BeanUtils.updateItem(pageList.list, vo);
						}
						Alert.show("密码重置成功！");
						if(callback != null){
							callback();
						}
					}
					
				}, 
				function(event:FaultEvent):void{
					trace(event.fault);
					Alert.show("密码重置失败！" + event.fault.faultString);
				});
			
			delegate.resetPassword(vo).addResponder(responder);
		}
	}
}